package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.PurchaseListGoodsService;
import com.atguigu.jxc.service.PurchaseListService;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.GsonJsonParser;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * description
 *
 * @author SZ
 * @date 2022/9/25/11:11
 */
@RestController
@RequestMapping("/purchaseListGoods")
public class PurchaseListGoodsController {
    @Autowired
    private PurchaseListGoodsService purchaseListGoodsService;
    @Autowired
    private PurchaseListService purchaseListService;
    @Autowired
    private GoodsService goodsService;
    @PostMapping("/save")
    @Transactional
    public ServiceVO save(PurchaseList purchaseList, String purchaseListGoodsStr, HttpSession session){
        //查看UserController看看登录的时候把user信息放哪了，放在了session里
        User user = (User) session.getAttribute ("currentUser");
        purchaseList.setUserId (user.getUserId ());
        //往:t_purchase_list添加记录
        purchaseListService.save(purchaseList);
        //查看pom依赖可知引入了Gson,利用Gson来完成将JSON字符串转化为实体类对象
        Gson gson = new Gson ();
//        PurchaseListGoods purchaseListGoods = gson.fromJson (purchaseListGoodsStr, PurchaseListGoods.class);
        List<Object> purchaseListGoodsList = gson.fromJson (purchaseListGoodsStr, List.class);
        //往:t_purchase_list_goods添加记录
        for (Object object : purchaseListGoodsList) {
            String json = gson.toJson (object);
            PurchaseListGoods purchaseListGoods = gson.fromJson (json, PurchaseListGoods.class);
            purchaseListGoods.setPurchaseListId (purchaseList.getPurchaseListId ());
            purchaseListGoodsService.save(purchaseListGoods);
            Integer goodsNum = purchaseListGoods.getGoodsNum ();
            Integer goodsId = purchaseListGoods.getGoodsId ();
            //修改库存:t_goods进行更新。往对应的商品goodsId上增加库存goodsNum。
            goodsService.addQuantity(goodsNum,goodsId);
        }

        return new ServiceVO (100, "请求成功");
    }
    @PostMapping("/list")
    public Map<String, Object> list(String purchaseNumber, Integer supplierId, Integer state, String sTime,String eTime){
        List<PurchaseList> purchaseListList = purchaseListService.list(purchaseNumber,supplierId,state,sTime,eTime);
        Map<String, Object> resultMap = new HashMap<> ();
        resultMap.put ("rows",purchaseListList);
        return resultMap;
    }
    @PostMapping("/goodsList")
    public List<PurchaseList> getGoodsList(Integer purchaseListId){
       return purchaseListGoodsService.getGoodsList(purchaseListId);
    }
    @PostMapping("/delete")
    public ServiceVO delete(Integer purchaseListId){
        return purchaseListService.deleteById(purchaseListId);
    }

}
